using UnityEngine;
using System.Collections;

public class SpinScript : MonoBehaviour {
	public float life = 0.0f;
	public NetworkPlayer owner;
	public Vector3 last;
	public Transform target;
	// Use this for initialization
	void Start () {
		last = new Vector3(99999.0f,99999.0f,99999.0f);
	
	}
	
	// Update is called once per frame
	void Update () {
		if(owner != null){
			life += Time.deltaTime;
			if(life >= 0.5f){
				GameObject.Destroy(gameObject);
			}
			if(Network.player == owner){
				transform.Rotate(new Vector3(0,45,0));
				transform.position = target.position;
				if(Vector3.Distance(last,transform.TransformPoint(transform.position)) > 10.0f){
					networkView.RPC("SpinPositionUpdate",RPCMode.Others, transform.position, transform.rotation);
					last = transform.position;
				}
			}
		}
	}
	void OnTriggerEnter(Collider other){
		EnemyHealth e = other.GetComponent<EnemyHealth>();
		if(e != null){
			e.damage(20.0f);
		}
	}
	public void setTarget(Transform par, NetworkPlayer o){
		target = par;
		networkView.RPC("setSpinOwner",RPCMode.All,o);
	}
	[RPC]
	void setSpinOwner(NetworkPlayer o){
		owner = o;
	}
	[RPC] 
	void SpinPositionUpdate(Vector3 pos, Quaternion rot){
		transform.position = pos;
		transform.rotation = rot;
	}
	[RPC]
	void destroySpin(){}
}
